package com.encyclopedia.service.impl;


import com.encyclopedia.entity.Cultivation;
import com.encyclopedia.mapper.CultivationMapper;
import com.encyclopedia.result.exception.ServiceException;
import com.encyclopedia.result.exenum.RequestException;
import com.encyclopedia.service.CultivationService;
import com.encyclopedia.service.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author MaLiang
 * @version 1.0
 * @date 2024-04-22 8:53
 */
@Service
public class CultivationServiceImpl implements CultivationService {

    @Autowired
    private CultivationMapper cultivationMapper;
    @Autowired
    private RedisUtils redisUtils;

    /**
     * 根据植物信息id获取种植信息
     *
     * @param plantId
     * @return
     */
    @Override
    public Cultivation getCultivationById(Integer plantId) {
        String key = "CultivationServiceImplByPlantId:" + plantId;
        Cultivation cultivation = (Cultivation) redisUtils.get(key);
        if (cultivation != null) return cultivation;
        Cultivation cultivationByPlantId = cultivationMapper.getCultivationByPlantId(plantId);
        redisUtils.set(key, cultivationByPlantId, 60 * 60 * 12);
        if (cultivationByPlantId == null) throw new ServiceException(RequestException.NOT_FOUND);
        return cultivationByPlantId;
    }
}
